package com.hualan.sfzyproject.service;


import com.hualan.sfzyproject.domain.PsyTrueReply;
import com.hualan.sfzyproject.mapper.PsyTrueReplyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class PsyTrueReplyService {
    @Autowired
    private PsyTrueReplyMapper psyTrueReplyMapper;

    //    根据id查询
    public List<PsyTrueReply> selectByQuestionId(int questionId) {
        return psyTrueReplyMapper.selectByQuestionId(questionId);
    }

    public int insert(@RequestParam("replyContent") String replyContent,
                      @RequestParam("userId") Integer userId,
                      @RequestParam("questionId") Integer questionId)
    {
        return psyTrueReplyMapper.insert(replyContent, userId, questionId);
    }

    public int insert(@RequestParam("replyContent") String replyContent,
                      @RequestParam("userId") Integer userId,
                      @RequestParam("questionId") Integer questionId,
                      @RequestParam("fatherReplyId") Integer fatherReplyId)
    {
        return psyTrueReplyMapper.inserts(replyContent, userId, questionId, fatherReplyId);
    }
    public List<PsyTrueReply> selectByUserId(int userId, int pageNum, int pageSize) {
        // Retrieve all records for the user
        List<PsyTrueReply> allReplies = psyTrueReplyMapper.selectByUserId(userId);
        int maxPage = (int) Math.ceil((double) allReplies.size() / pageSize);

        if (allReplies.isEmpty() || pageNum > maxPage) {
            return null;
        }

        // Calculate the starting and ending index for the page
        int start = Math.max(0, (pageNum - 1) * pageSize);
        int end = Math.min(allReplies.size(), start + pageSize);

        // Return the sublist, making sure not to throw IndexOutOfBoundsException
        return allReplies.subList(start, end);
    }

    //模糊搜索
    public List<PsyTrueReply> selectByLike(String like){
        return psyTrueReplyMapper.selectByLike(like);
    }

    public int deleteByReplyId(int replyId) {
        return psyTrueReplyMapper.deleteByReplyId(replyId);
    }

    public int deleteByQuestionId(int questionId) {
        return psyTrueReplyMapper.deleteByQuestionId(questionId);
    }

    //    搜索全部
    public List<PsyTrueReply> getAll() {
        return psyTrueReplyMapper.selectAll();
    }


}
